<%@page import="java.util.List" %>
<%@ page import="com.web.test.dao.MessageDao" %>
<%@ page import="com.web.test.bean.Message" %>
<%@ page import="com.web.test.utils.SQLUtils" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>获取数据</title>
    <style>
        td {
            width: 260px;
        }
    </style>
</head>
<%
    String isLogin = (String) session.getAttribute("isLogin");
    boolean isL = false;
    List<Message> messages = null;
    int totalCount = 0;
    //多少页
    int pageNumber = 0;

    int currentPage = 1;
    //每页多少条记录
    String pageCount = null;
    ;


    if (isLogin == null) {
        String name = request.getParameter("name");
        if (name != null && name != "" && name.equals("zcy")) {

            isL = true;
            session.setAttribute("isLogin", "true");
            out.print("<h1>登录成功</h1>");

        } else {
            System.out.println("======");
            session.setAttribute("isLogin", "false");
            response.sendRedirect("index.jsp");
        }


    }

    if (isL | (isLogin != null && isLogin.equals("true"))) {

        MessageDao mDao = new MessageDao();


        pageCount = request.getParameter("pageCount");

        if (pageCount == null && session.getAttribute("pageCount") == null) {

            session.setAttribute("pageCount", "4");

        } else {

            if (pageCount != null) {

                session.setAttribute("pageCount", pageCount);
            }
        }


        if (session.getAttribute("page") == null) {

            session.setAttribute("page", 1);

        } else {

            if (request.getParameter("whichPage") != null) {

                currentPage = Integer.parseInt(request.getParameter("whichPage"));
            }
            session.setAttribute("page", currentPage);


        }

        pageCount = (String) session.getAttribute("pageCount");
        messages = mDao.page(Integer.parseInt(pageCount), (Integer) session.getAttribute("page"));

        List<Message> totalMessages = mDao.findAndSort(SQLUtils.all);
        totalCount = totalMessages.size();

        pageNumber = (int) Math.ceil((float) totalCount / Float.valueOf(pageCount));

    }

%>
<body>

<div>
    <table>

        <tr>
            <td>序号</td>
            <td>姓名</td>
            <td>年龄</td>
            <td>性别</td>
            <td>电话号码</td>
            <td>地址</td>
        </tr>
        <%
            if (messages != null) {

                for (Message message : messages) { %>

        <tr>
            <td><%=message.getId()%>#</td>
            <td><%=message.getName() %>
            </td>
            <td><%=message.getAge() %>
            </td>
            <td><%=message.getSex() %>
            </td>
            <td><%=message.getPhone() %>
            </td>
            <td><%=message.getLoc() %>
            </td>
            <%--<td><a href="">删除</a></td>--%>
        </tr>

        <%
            }
        } else {%>

        <tr>
            <td>暂无数据</td>
        </tr>
        <%
            }

        %>

        <tr>
            <td>总共<%=totalCount%>条记录</td>
            <td>分<%=pageNumber%>页
            </td>
            <td>每页显示<input type="text" style="width: 30px" value="<%=pageCount%>"
                           onkeydown="page(this,<%=totalCount%>)">数据
            </td>

            <td onclick="last_page(<%=session.getAttribute("page")%>)" id="last" align="center">
                <a href="#"> 上一页</a>
            </td>
            <td>
                <%

                    for (int i = 0; i < pageNumber; i++) {%>

                [<a href="get_data.jsp?whichPage=<%=(i+1)%>"><%=(i + 1)%>]
                </a>
                <%
                    }
                %>

            </td>

            <td id="next"><input type="text" id="input_page" style="width: 40px">
                <button onclick="jump_page(<%=pageNumber%>)">跳转</button>
            </td>

            <td onclick="next_page(<%=session.getAttribute("page")%>,<%=pageNumber%>)"><a href="#"> 下一页</a></td>
        </tr>


    </table>
</div>

<script type="text/javascript">

    function jump_page(pageC) {

        var input_page = document.getElementById("input_page").value;

        if (input_page == null || input_page == "") {
            alert("不能输入空值");
        } else {
            if (input_page > pageC) {
                alert("最多只能输入" + pageC);
            } else if (input_page > 0 && input_page <= pageC) {

                window.location = "get_data.jsp?whichPage=" + input_page;
            } else {
                alert("不能输入负值");
            }

        }

    }

    function last_page(currentPage) {

        if (currentPage == 1) {
            alert("当前已是第一页");
        } else {
            window.location = "get_data.jsp?whichPage=" + (currentPage - 1);
        }

    }

    function next_page(currentPage, pageC) {

        if (currentPage == pageC) {
            alert("当前已是最后一页");
        } else {
            window.location = "get_data.jsp?whichPage=" + (currentPage + 1);
        }

    }

    function page(i, totalCount) {

        if (event.keyCode == 13) {

            if (i.value <= 0) {
                alert("不能输入负值和0");
            } else if (i.value <= totalCount) {

                window.location = "get_data.jsp?pageCount=" + i.value;

            } else {
                alert("不能大于总的记录数");
            }

        }
    }

</script>
</body>

</html>